from ..models.base_model import BaseModel
from ..models.student import Student
from django.contrib.auth.models import User
from django.db import models

class LeaveRequest(BaseModel):
    """请假申请"""
    student = models.ForeignKey(Student, on_delete=models.CASCADE, related_name='leave_requests', verbose_name="学生")
    start_date = models.DateField(verbose_name="开始日期")
    end_date = models.DateField(verbose_name="结束日期")
    reason = models.TextField(verbose_name="请假原因")
    leave_type = models.CharField(
        max_length=20, 
        choices=[
            ('sick', '病假'),
            ('personal', '事假'),
            ('other', '其他'),
        ],
        verbose_name="请假类型"
    )
    attachment = models.FileField(upload_to='leave_requests/', blank=True, null=True, verbose_name="附件")
    status = models.CharField(
        max_length=20, 
        choices=[
            ('pending', '待审批'),
            ('approved', '已批准'),
            ('rejected', '已拒绝'),
        ],
        default='pending',
        verbose_name="状态"
    )
    applied_at = models.DateTimeField(auto_now_add=True, verbose_name="申请时间")
    reviewed_by = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True, related_name='reviewed_leave_requests', verbose_name="审批人")
    reviewed_at = models.DateTimeField(null=True, blank=True, verbose_name="审批时间")
    review_comment = models.TextField(blank=True, verbose_name="审批意见")

    def __str__(self):
        return f"{self.student} - {self.start_date} 至 {self.end_date}"

    class Meta:
        verbose_name = "请假申请"
        verbose_name_plural = "请假申请"